package gqz.testdemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.michaelevans.colorart.library.ColorArt;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/8/29.
 */

public class PaletteDemo extends Activity {

    String TAG = "PaletteDemo-->";

    ImageView iv;
    TextView tv0, tv1, tv2, tv3, tv4, tv5, tv6;
    TextView tv11, tv12, tv13, tv14;
    ListView lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_palette);

        iv = findViewById(R.id.palette_iv);
        tv0 = findViewById(R.id.palette_tv0);
        tv1 = findViewById(R.id.palette_tv1);
        tv2 = findViewById(R.id.palette_tv2);
        tv3 = findViewById(R.id.palette_tv3);
        tv4 = findViewById(R.id.palette_tv4);
        tv5 = findViewById(R.id.palette_tv5);
        tv6 = findViewById(R.id.palette_tv6);
        tv11 = findViewById(R.id.palette_tv11);
        tv12 = findViewById(R.id.palette_tv12);
        tv13 = findViewById(R.id.palette_tv13);
        tv14 = findViewById(R.id.palette_tv14);
        lv = findViewById(R.id.palette_lv);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.lx);
        iv.setImageDrawable(new BitmapDrawable(getResources(), bmp));
//        TestColorArt(bmp);
        TestPalette(bmp);
    }

    public void TestColorArt(Bitmap bmp) {
        ColorArt art = new ColorArt(bmp);
        tv11.setBackgroundColor(art.getPrimaryColor());
        tv12.setBackgroundColor(art.getSecondaryColor());
        tv13.setBackgroundColor(art.getDetailColor());
        tv14.setBackgroundColor(art.getBackgroundColor());
    }

    public void TestPalette(Bitmap bmp) {
        Palette.from(bmp).maximumColorCount(8).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {

                ArrayList<Palette.Swatch> list = new ArrayList<>();
                list.addAll(palette.getSwatches());
                PaletteAdapter adapter = new PaletteAdapter(PaletteDemo.this, list, palette.getDominantSwatch().getPopulation());
                lv.setAdapter(adapter);

                /*Palette.Swatch main = palette.getDominantSwatch();

                Palette.Swatch sv = palette.getVibrantSwatch();
                Palette.Swatch sv_d = palette.getDarkVibrantSwatch();
                Palette.Swatch sv_l = palette.getLightVibrantSwatch();

                Palette.Swatch sm = palette.getMutedSwatch();
                Palette.Swatch sm_d = palette.getDarkMutedSwatch();
                Palette.Swatch sm_l = palette.getLightMutedSwatch();

                if (main != null) {
                    tv0.setBackgroundColor(main.getRgb());
                    tv0.setTextColor(main.getBodyTextColor());
                    tv0.append(" population:" + main.getPopulation());
                } else Log.e(TAG, "onGenerated: main is null");

                if (sv != null) {
                    tv1.setBackgroundColor(sv.getRgb());
                    tv1.setTextColor(sv.getBodyTextColor());
                    tv1.append(" population:" + sv.getPopulation());
                } else Log.e(TAG, "onGenerated: sv is null");
                if (sv_d != null) {
                    tv2.setBackgroundColor(sv_d.getRgb());
                    tv2.setTextColor(sv_d.getBodyTextColor());
                    tv2.append(" population:" + sv_d.getPopulation());
                } else Log.e(TAG, "onGenerated: sv_d is null");
                if (sv_l != null) {
                    tv3.setBackgroundColor(sv_l.getRgb());
                    tv3.setTextColor(sv_l.getBodyTextColor());
                    tv3.append(" population:" + sv_l.getPopulation());
                } else Log.e(TAG, "onGenerated: sv_l is null");

                if (sm != null) {
                    tv4.setBackgroundColor(sm.getRgb());
                    tv4.setTextColor(sm.getBodyTextColor());
                    tv4.append(" population:" + sm.getPopulation());
                } else Log.e(TAG, "onGenerated: sm is null");
                if (sm_d != null) {
                    tv5.setBackgroundColor(sm_d.getRgb());
                    tv5.setTextColor(sm_d.getBodyTextColor());
                    tv5.append(" population:" + sm_d.getPopulation());
                } else Log.e(TAG, "onGenerated: sm_d is null");
                if (sm_l != null) {
                    tv6.setBackgroundColor(sm_l.getRgb());
                    tv6.setTextColor(sm_l.getBodyTextColor());
                    tv6.append(" population:" + sm_l.getPopulation());
                } else Log.e(TAG, "onGenerated: sm_l is null");*/
            }
        });

    }

    class PaletteAdapter extends BaseAdapter {

        Context context;
        ArrayList<Palette.Swatch> list;
        int mainPopulation = 0;

        public PaletteAdapter(Context context, ArrayList<Palette.Swatch> list, int mainPopulation) {
            this.context = context;
            this.list = list;
            this.mainPopulation = mainPopulation;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_palette, null);
                holder.lay = convertView.findViewById(R.id.item_palette_rootlay);
                holder.tv_title = convertView.findViewById(R.id.item_palette_tv_title);
                holder.tv_body = convertView.findViewById(R.id.item_palette_tv_body);
                holder.tv_count = convertView.findViewById(R.id.item_palette_tv_count);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Palette.Swatch sw = list.get(position);
            if (sw != null) {
//                holder.lay.setBackgroundColor(sw.getRgb());
                convertView.setBackgroundColor(sw.getRgb());
                holder.tv_title.setTextColor(sw.getTitleTextColor());
                holder.tv_body.setTextColor(sw.getBodyTextColor());
                holder.tv_count.setText(" population:" + sw.getPopulation() + "  #" + (position + 1));
                if (mainPopulation == sw.getPopulation())
                    holder.tv_count.setBackgroundColor(Color.argb(100, 150, 150, 150));
                else holder.tv_count.setBackgroundColor(Color.TRANSPARENT);
            }

            return convertView;
        }

        class ViewHolder {
            LinearLayout lay;
            TextView tv_title, tv_body, tv_count;
        }
    }
}
